var prefix = jsctx + "/hlhtTenantOpenList";

var hlhtDiscount = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        }, 10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });

    function load() {

        var tenantType = $("#tenantType").val();

        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listPageHlhtDiscount",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    cache: false,
                    pageSize: 20,
                    pageList: [20],
                    pageNumber: 1,
                    sidePagination: "server",
                    queryParams: function (params) {
                        return {
                            limit: params.limit,
                            offset: (params.offset / params.limit) + 1,
                            memberId: $("#memberId").val().trim(),
                            tenantCode: $("#tenantCode").val().trim()
                        };
                    },
                    columns: [
                        {
                            checkbox: true, field: '', width: '50', align: 'left',
                        },
                        {
                            title: '#', field: '', width: '50',
                            formatter: function (value, row, index) {
                                return index + 1;
                            }
                        },
                        {field: 'tenantName', title: '名称', width: '100', align: 'left'},
                        {field: 'mobile', title: '手机号', width: '110', align: 'left'},
                        {
                            field: '', title: '互联运营商名称', width:'120',align:'left',
                            formatter: function (value, row, index) {
                                if (row.hlhtOperatorName == '') {
                                    return row.hlhtTenantName;
                                } else if (row.hlhtOperatorName == null) {
                                    return row.hlhtTenantName;
                                } else {
                                    return row.hlhtOperatorName;
                                }
                            }
                        },
                        {field: 'stationName', title: '桩群名称', width: '100', align: 'left'},
                        {
                            field: 'discountType',
                            title: '折扣类型',
                            width: '100', align: 'left',
                            formatter: function (value, row, index) {
                                if (row.discountType == '0') {
                                    return '<span >服务费折扣</span>';
                                } else if (row.discountType == '1') {
                                    return '<span >总价折扣</span>';
                                }
                            }
                        },
                        {
                            field: '', title: '服务费折扣', width: '90', align: 'right',
                            formatter: function (value, row, index) {
                                if (row.discountType == '0') {
                                    var z = '';
                                    for (var i = 0; i < row.discountTimeSaveVoList.length; i++) {
                                        var a = row.discountTimeSaveVoList[i].discount;
                                        z += a + '<br>';
                                    }
                                    return z;
                                } else {
                                    return '-'
                                }
                            }
                        },
                        {
                            field: '', title: '电费优惠价', width: '90', align: 'right',
                            formatter: function (value, row, index) {
                                if (row.discountType == '1') {
                                    var z = '';
                                    for (var i = 0; i < row.discountTimeSaveVoList.length; i++) {
                                        var a = row.discountTimeSaveVoList[i].elecPrice;
                                        z += a + '<br>';
                                    }
                                    return z;
                                } else {
                                    return '-'
                                }
                            }
                        },
                        {
                            field: '', title: '服务费优惠价', width: '100', align: 'right',
                            formatter: function (value, row, index) {
                                if (row.discountType == '1') {
                                    var z = '';
                                    for (var i = 0; i < row.discountTimeSaveVoList.length; i++) {
                                        var a = row.discountTimeSaveVoList[i].servicePrice;
                                        z += a + '<br>';
                                    }
                                    return z;
                                } else {
                                    return '-'
                                }
                            }
                        },
                        {
                            field: '', title: '时段', width: '130', align: 'left',
                            formatter: function (value, row, index) {
                                var z = '';
                                for (var i = 0; i < row.discountTimeSaveVoList.length; i++) {
                                    var a = row.discountTimeSaveVoList[i].startPeriod;
                                    var b = '~';
                                    var c = row.discountTimeSaveVoList[i].endPeriod;
                                    z += a + ' ' + b + ' ' + c + '<br>';

                                }
                                return z;
                            }
                        },
                        {
                            field:'',title:'活动有效时间',width:'200',align:'left',
                            formatter:function (value,row,index) {
                                return row.beginTime + '至' + row.endTime
                            }
                        },
                        {field: 'status', title: '状态', width: '100', align: 'left'},
                        {field: 'policyDesc', title: '备注', width: '100', align: 'left'},
                        {
                            title: '操作',
                            field: 'roleId',
                            align: 'center',
                            width: '200',
                            formatter: function (value, row, index) {

                                var b = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#" mce_href="#" title="编辑" onclick="hlhtDiscount.editHlhtDiscount(\''
                                    + row.discountStationId + '\',' + '\'' + tenantType +
                                    '\')">编辑</a> ';

                                var c = '<a class="btn btn-danger btn-sm ' + has_edit + '" href="#" mce_href="#" title="删除" onclick="hlhtDiscount.deleteHlhtDiscountStation(\''
                                    + row.discountStationId + '\')">删除</a> ';

                                return b + c;

                            }
                        }
                    ]
                });
    }

    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
    }

    function goBack() {
        window.history.back();
    }

    function addHlhtDiscount(tenantCode, hlhtMemberId, tenantType) {
        location.href = prefix + "/addHlhtDiscount/" + tenantCode + "/" + hlhtMemberId + "/" + tenantType;
    }

    function editHlhtDiscount(discountStationId,tenantType) {
        location.href = prefix + "/editHlhtDiscount/" + discountStationId + "/" + tenantType;
    }


    function deleteHlhtDiscountStation(discountStationId) {
        layer.confirm('确定要删除选中的记录？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/deleteHlhtDiscountStation",
                type: "post",
                data: {
                    'discountStationId': discountStationId
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("删除成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }

    //批量删除站点
    function selectDiscountStation() {
        var rows = $("#exampleTable").bootstrapTable('getSelections');// 获得要删除的数据
        if (rows.length == 0) {// rows 主要是为了判断是否选中，下面的else内容才是主要
            alert("请先选择站点!");
            return;
        } else {
            var discountStationList = new Array();// 声明一个数组
            $(rows).each(function () {// 通过获得别选中的来进行遍历
                discountStationList.push(this.discountStationId); // cid为获得到的整条数据中的一列
            });
            deleteBatchHlhtDiscountStation(discountStationList)
        }
    }

    function deleteBatchHlhtDiscountStation(discountStationList) {
        layer.confirm('确定要删除选中站点？', {
            title: '删除站点折扣', content: '批量删除站点折扣', btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/deleteBatchHlhtDiscountStation",
                type: "post",
                traditional: true,
                data: {
                    discountStationList: discountStationList,
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("删除成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }


    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        addHlhtDiscount: addHlhtDiscount,
        editHlhtDiscount: editHlhtDiscount,
        deleteHlhtDiscountStation: deleteHlhtDiscountStation,
        selectDiscountStation: selectDiscountStation
    };
})();


